package com.jetlion.demo2;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashSet;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] language = {"C++", "JAVA", "Python", "Ruby", "PHP"};

        Button button = findViewById(R.id.btn);

        TextView textView = findViewById(R.id.languageText);

        //弹窗构建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        //用来存储课程的集合，HashSet能去除重复
        HashSet<String> set = new HashSet<>();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                builder.setTitle("请选择你兴趣的语言");

                //点击和取消 选项的事件
                builder.setMultiChoiceItems(language, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        if (b) {
                            set.add(language[i]);

                        } else {
                            set.remove(language[i]);
                        }
                    }
                });

                //确定的按钮
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.i("MainActivity", set.toString());
                        textView.setText(set.toString());
                        //集合内的数据移除，避免重复
                        set.clear();
                    }
                });

                //取消的按钮
                builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //集合内的数据移除，避免重复
                        set.clear();
                    }
                });

                //显示弹窗
                builder.create().show();
            }
        });
    }
}